using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;


namespace eHouse
{
    public class TemperatureWindow:Window
    {
        private Window temperatureWindow;
        Canvas canvas = new Canvas();
        double temperature;
        Text text_temp;
        SimpleButton button_heat_on;
        SimpleButton button_heat_off;
        SimpleButton button_cold_on;
        SimpleButton button_cold_off;

        public TemperatureWindow(Window mw)
        {
            temperatureWindow = new Window();
            temperatureWindow.Height = SystemMetrics.ScreenHeight;
            temperatureWindow.Width = SystemMetrics.ScreenWidth;          
            
            CreateWindow();                                    
        }

        void CreateWindow()
        {
            temperature = TemperatureBL.GetInstance().GetTemperature();

            text_temp = new Text();
            text_temp.Font = Resources.GetFont(Resources.FontResources.NinaB);
            text_temp.HorizontalAlignment = HorizontalAlignment.Center;
            text_temp.VerticalAlignment = VerticalAlignment.Center;
            text_temp.TextContent = "The temperature is: "+temperature;
            text_temp.Height = temperatureWindow.Height / 4;
            Canvas.SetTop(text_temp, 0);
            Canvas.SetLeft(text_temp, 0);
            canvas.Children.Add(text_temp);

            Image image_update = new Image(Resources.GetBitmap(Resources.BitmapResources.update));
            image_update.HorizontalAlignment = HorizontalAlignment.Center;
            image_update.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_update = new SimpleButton(image_update, temperatureWindow.Width/4, temperatureWindow.Height / 5);
            button_update.BackColor = Colors.White;
            Canvas.SetTop(button_update, 0);
            Canvas.SetRight(button_update, 0);
            canvas.Children.Add(button_update);

            Image image_heat_on = new Image(Resources.GetBitmap(Resources.BitmapResources.heat));
            image_heat_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_heat_on.VerticalAlignment = VerticalAlignment.Center;
            button_heat_on = new SimpleButton(image_heat_on, 55, 55);
            button_heat_on.BackColor = Colors.White;

            Image image_heat_off = new Image(Resources.GetBitmap(Resources.BitmapResources.heat_off));
            image_heat_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_heat_off.VerticalAlignment = VerticalAlignment.Center;
            button_heat_off = new SimpleButton(image_heat_off, 55, 55);
            button_heat_off.BackColor = Colors.White;

            Image image_cold_on = new Image(Resources.GetBitmap(Resources.BitmapResources.cold));
            image_cold_on.HorizontalAlignment = HorizontalAlignment.Center;
            image_cold_on.VerticalAlignment = VerticalAlignment.Center;
            button_cold_on = new SimpleButton(image_cold_on, 55, 55);
            button_cold_on.BackColor = Colors.White;

            Image image_cold_off = new Image(Resources.GetBitmap(Resources.BitmapResources.cold_off));
            image_cold_off.HorizontalAlignment = HorizontalAlignment.Center;
            image_cold_off.VerticalAlignment = VerticalAlignment.Center;
            button_cold_off = new SimpleButton(image_cold_off, 55, 55);
            button_cold_off.BackColor = Colors.White;

            bool[] heatingAC = TemperatureBL.GetInstance().GetHeatingAC();

            if (heatingAC[0])
            {
                Canvas.SetTop(button_heat_on, temperatureWindow.Height / 2);
                Canvas.SetLeft(button_heat_on, 0);
                canvas.Children.Add(button_heat_on);
            }
            else
            {
                Canvas.SetTop(button_heat_off, temperatureWindow.Height / 2);
                Canvas.SetLeft(button_heat_off, 0);
                canvas.Children.Add(button_heat_off);
            }

            if (heatingAC[1])
            {
                Canvas.SetTop(button_cold_on, temperatureWindow.Height / 2);
                Canvas.SetLeft(button_cold_on, 100);
                canvas.Children.Add(button_cold_on);
            }
            else
            {
                Canvas.SetTop(button_cold_off, temperatureWindow.Height / 2);
                Canvas.SetLeft(button_cold_off, 100);
                canvas.Children.Add(button_cold_off);
            }
            Image image_schedule = new Image(Resources.GetBitmap(Resources.BitmapResources.schedule));
            image_schedule.HorizontalAlignment = HorizontalAlignment.Center;
            image_schedule.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_schedule = new SimpleButton(image_schedule, temperatureWindow.Width / 2, temperatureWindow.Height / 5);
            button_schedule.BackColor = Colors.White;
            Canvas.SetBottom(button_schedule, 0);
            Canvas.SetLeft(button_schedule, 0);
            canvas.Children.Add(button_schedule);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, temperatureWindow.Width / 2, temperatureWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);


            button_heat_on.Click += new EventHandler(buttonHeatOn_Click);
            button_heat_off.Click += new EventHandler(buttonHeatOff_Click);
            button_cold_on.Click += new EventHandler(buttonColdOn_Click);
            button_cold_off.Click += new EventHandler(buttonColdOff_Click);
            button_return.Click += new EventHandler(returnButton_Click);
            button_update.Click += new EventHandler(updateButton_Click);
            button_schedule.Click += new EventHandler(scheduleButton_Click);

            temperatureWindow.Child = canvas;
            temperatureWindow.Visibility = Visibility.Visible;
        }


        void buttonHeatOn_Click(object sender, EventArgs evt)
        {
            TemperatureBL.GetInstance().SetHeatingAC("Heating", false);            
            Debug.Print("Switching off heat");
            canvas.Children.Remove(button_heat_on);

            Canvas.SetTop(button_heat_off, temperatureWindow.Height / 2);
            Canvas.SetLeft(button_heat_off, 0);
            canvas.Children.Add(button_heat_off);
        }

        void buttonHeatOff_Click(object sender, EventArgs evt)
        {
            if (!TemperatureBL.GetInstance().GetHeatingAC()[1])
            {
                TemperatureBL.GetInstance().SetHeatingAC("Heating", true);
                Debug.Print("Switching on heat");
                canvas.Children.Remove(button_heat_off);

                Canvas.SetTop(button_heat_on, temperatureWindow.Height / 2);
                Canvas.SetLeft(button_heat_on, 0);
                canvas.Children.Add(button_heat_on);
            }
        }

        void buttonColdOn_Click(object sender, EventArgs evt)
        {
            TemperatureBL.GetInstance().SetHeatingAC("AC", false);  
            Debug.Print("Switching off AC");
            canvas.Children.Remove(button_cold_on);

            Canvas.SetTop(button_cold_off, temperatureWindow.Height / 2);
            Canvas.SetLeft(button_cold_off, 100);
            canvas.Children.Add(button_cold_off);
        }

        void buttonColdOff_Click(object sender, EventArgs evt)
        {
            if (!TemperatureBL.GetInstance().GetHeatingAC()[0])
            {
                TemperatureBL.GetInstance().SetHeatingAC("AC", true);
                Debug.Print("Switching on AC");
                canvas.Children.Remove(button_cold_off);

                Canvas.SetTop(button_cold_on, temperatureWindow.Height / 2);
                Canvas.SetLeft(button_cold_on, 100);
                canvas.Children.Add(button_cold_on);
            }
        }

        void returnButton_Click(object sender, EventArgs evt)
        {
            temperatureWindow.Close();
        }

        void updateButton_Click(object sender, EventArgs evt)
        {
            Debug.Print("Update button pressed");
            temperature = TemperatureBL.GetInstance().GetTemperature();
            text_temp.TextContent = "The temperature is: " + temperature;
        }

        void scheduleButton_Click(object sender, EventArgs evt)
        {
            Debug.Print("Schedule button pressed");
            TemperatureScheduleWindow tsw = new TemperatureScheduleWindow();
        }
    }
}
